package com.fr.report.script.function;

import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.core.FArray;
import com.fr.report.script.core.FunctionHelper;

public class SUM extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    double d = 0.0D;
    for (int i = 0; i < paramArrayOfObject.length; ++i)
    {
      if (paramArrayOfObject[i] == null)
        break label33:
      d += parseObject(paramArrayOfObject[i]);
    }
    label33: return FunctionHelper.parsePrimitiveDouble(d);
  }

  private double parseObject(Object paramObject)
  {
    if (paramObject instanceof Number)
      return ((Number)paramObject).doubleValue();
    if (paramObject instanceof Boolean)
      return ((((Boolean)paramObject).booleanValue()) ? 1.0D : 0.0D);
    if (paramObject instanceof FArray)
    {
      FArray localFArray = (FArray)paramObject;
      double d = 0.0D;
      int i = 0;
      int j = localFArray.length();
      while (i < j)
      {
        d += parseObject(localFArray.elementAt(i));
        ++i;
      }
      return d;
    }
    if (paramObject != null)
      try
      {
        return Double.parseDouble(paramObject.toString());
      }
      catch (NumberFormatException localNumberFormatException)
      {
        return 0.0D;
      }
    return 0.0D;
  }

  public Function.Type getType()
  {
    return Function.MATH;
  }

  public String getCN()
  {
    return "SUM(number1,number2,…): 求一个指定单元格区域中所有数字之和。\nNumber1,number2,…:1到30个参数或指定单元格区域中所有数字。\n备注:\n    函数将直接键入参数中的数值、逻辑值及文本表达式计算在内。\n    若参数是数组或引用，则只有数组或单元格引用中的数值进行计算。\n示例:\nSUM(70,80)等于150。\nSUM(\"70\",80,TRUE)等于151，逻辑值“TRUE”作为1来计算；“FALSE”作为0计算；文本“70”作为70来计算。";
  }

  public String getEN()
  {
    return "SUM(number1,number2,…): Adds all the numbers in a range of cells.\nNumber1, number2, ...    are 1 to 30 arguments for which you want the total value or sum.\n\nRemarks:\n1. Numbers, logical values, and text representations of numbers that you type directly into the list of arguments are counted. See the first and second examples following. \n2. If an argument is an array or reference, only numbers in that array or reference are counted. Empty cells, logical values, text, or error values in the array or reference are ignored. See the third example following. \n\nExample:\n   SUM(70,80)=150.\n   SUM(\"70\",80,TRUE)=151, TRUE is calculated as 1, FALSE for 0, String \"70\" fo 70.";
  }
}